﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Hr_BlackList。
    /// </summary>
    public class Hr_BlackList
    {
        public Hr_BlackList()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Hr_BlackList model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_BlackList(");
            strSql.Append("RecordId,UserName,Tel,Mobile,Email,Address,AddCompany,AddDepart,AddUser,Level,UpdateTime,Initial)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@UserName,@Tel,@Mobile,@Email,@Address,@AddCompany,@AddDepart,@AddUser,@Level,@UpdateTime,@Initial)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "Tel", DbType.AnsiString, model.Tel);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "AddCompany", DbType.AnsiString, model.AddCompany);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "AddUser", DbType.AnsiString, model.AddUser);
            db.AddInParameter(dbCommand, "Level", DbType.Int32, model.Level);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "Initial", DbType.AnsiString, model.Initial);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Hr_BlackList model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Hr_BlackList set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Tel=@Tel,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Email=@Email,");
            strSql.Append("Address=@Address,");
            strSql.Append("AddCompany=@AddCompany,");
            strSql.Append("AddDepart=@AddDepart,");
            strSql.Append("AddUser=@AddUser,");
            strSql.Append("Level=@Level,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("Initial=@Initial");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "Tel", DbType.AnsiString, model.Tel);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "AddCompany", DbType.AnsiString, model.AddCompany);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "AddUser", DbType.AnsiString, model.AddUser);
            db.AddInParameter(dbCommand, "Level", DbType.Int32, model.Level);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "Initial", DbType.AnsiString, model.Initial);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_BlackList ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_BlackList GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserName,Tel,Mobile,Email,Address,AddCompany,AddDepart,AddUser,Level,UpdateTime,Initial from Hr_BlackList ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.Hr_BlackList model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserName,Tel,Mobile,Email,Address,AddCompany,AddDepart,AddUser,Level,UpdateTime,Initial ");
            strSql.Append(" FROM Hr_BlackList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Hr_BlackList> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserName,Tel,Mobile,Email,Address,AddCompany,AddDepart,AddUser,Level,UpdateTime,Initial ");
            strSql.Append(" FROM Hr_BlackList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Hr_BlackList> list = new List<Chenxin.Lib.Hr.Entity.Hr_BlackList>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_BlackList ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Hr_BlackList model = new Chenxin.Lib.Hr.Entity.Hr_BlackList();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            model.UserName = dataReader["UserName"].ToString();
            model.Tel = dataReader["Tel"].ToString();
            model.Mobile = dataReader["Mobile"].ToString();
            model.Email = dataReader["Email"].ToString();
            model.Address = dataReader["Address"].ToString();
            model.AddCompany = dataReader["AddCompany"].ToString();
            model.AddDepart = dataReader["AddDepart"].ToString();
            model.AddUser = dataReader["AddUser"].ToString();
            ojb = dataReader["Level"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Level = (int)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            model.Initial = dataReader["Initial"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

